%% set parameter
s = 1; %% start point;
e = 10; %% end point;
L = 3000;%% time interval;
m = 8;%% muscle number;
fs = 2000; %%sampling rate.
fnyq = fs/2;
lowpass_frequency = 100/fnyq; %% decided by FFT. set the cutoff frequency.
highpass_frequency = 10/fnyq; %% set the cuttoff frequency.
%% set up time vector;
for i = 1:L
time(i,1) = i / fs;
end
%% plot M2_Y
for i = s:e
filename = sprintf('/Users/chenzhi/cz.ongoing/AOP_conference/M2_trials/%d_M2.csv',i)
demo_data = importdata(filename);
demo_data = demo_data.data(:,4);
%% find the onset point
max_point = max(demo_data);
ans = find(demo_data>0.2 * max_point);
onset = ans(1,1);
n = 20 * onset;
%% aligning.
filename = sprintf('/Users/chenzhi/cz.ongoing/AOP_conference/sEMG_trials/%d_EMG.csv',i);
raw_data = importdata(filename);
data = raw_data.data(n-1000:n+2000,1:8);
 %% 4th order highpass filter.
[b,a] = butter (4,highpass_frequency, 'high');
 for j = 1:m
   data(:,j) = filtfilt(b,a,data(:,j));
 end
%% full wave rectification
 for k =1:m
   data(:,k) = abs(data(:,k));
 end 
%% 4th lowpass filter.
[b,a] = butter(4,lowpass_frequency, 'low');
for l = 1:m
   data(:,l) = filtfilt(b,a,data(:,l));
end
save_filename = sprintf('/Users/chenzhi/cz.ongoing/AOP_conference/aligned_data/%d_aligned_data',i);
 xlswrite(save_filename,data);
end



